/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var $validation = {
    is_error: false,
    errors: {
        required: 'Field is required.',
        is_natural: 'Field is natural number.',
        is_natural_no_zero: 'Field is natural number and no zero.',
    },
    init: function() {
    },
    run: function($form, $field) {
        $('#' + $form).submit(function() {
            //Reset error to false.
            this.is_error = false;
            //Empty message output before.
            $validation.empty_message($form);
            //Traverse field to validate.
            for (var $x in $field) {
                $str = $('#' + $x).val();
                $rules = $field[$x].split('|');

                for (var $rule in $rules) {
                    if (!$validation.test($str, $rules[$rule])) {
                        $('#' + $x).after('<div class="validation">' + $validation.errors[$rules[$rule]] + '</div>');
                        this.is_error = true;
                        break;
                    }
                }
            }

            if (this.is_error) {
                return false;
            }

            return true;
        });
    },
    is_natural: function($str) {
        return $str.match(/^[0-9]+$/);
    },
    required: function($str) {
        if ($pwater.trim($str)) {
            return true;
        }

        return false;
    },
    is_natural_no_zero: function($str) {
        if (!$str.match('/^[0-9]+$/')) {
            return false;
        }

        if ($str == 0) {
            return false;
        }

        return false;
    },
    test: function($str, $rule) {
        switch ($rule) {
            case 'is_natural':
                return this.is_natural($str);
                break;
            default:
                return this.required($str);
                break;
        }
    },
    empty_message: function($form) {
        $('#' + $form + ' .validation').remove();
    }
};


$validation.run('add_area_form', {
    area_name: 'required',
    area_seq: 'required|is_natural'
});